/*
 * author xixi
 * date : 2016-6-26
 * use example: cjax.post();
 * */
var cajax = function(){
	var method = "GET";
	var params = {};
	var callback = function(){console.log('ajax succ')};
	var errCallback = function(){console.log('ajax fail')};
	
	this.get = function(newUrl,newParam,newCallback){
		castingAjax(newUrl
				,"GET"
				,newParam?newParam:params
				,newCallback?newCallback:callback);
	};
	this.post = function(newUrl,newParam,newCallback,isJson){
		if(isJson){
			castingAjax(newUrl
					,"POST"
					,JSON.stringify(newParam?newParam:params)
					,newCallback?newCallback:callback
					,errCallback
					,"application/json"
					,false);
		}else{
			castingAjax(newUrl
					,"POST"
					,newParam?newParam:params
					,newCallback?newCallback:callback
					,errCallback);
		}		
	};
	function castingAjax(url,method,params,callback,errCallback,contentType,processData){
		if(!url){
			return false;
		}
		$.ajax({
			type: method?method:"get",
			url:url,
			data:params,
			dataType:"json",
			processData:processData?processData:true,
			contentType:contentType?contentType:"application/x-www-form-urlencoded",
			headers : {'CASTING_TOKEN':$.cookie('CASTING_TOKEN')},
			success:function(data){
				if(callback) callback(data);
			},
			error:function(){
				if(errCallback) errCallback();
			}
		});
	}
	return this;
}();

function parseCastingDate(d,f){
	if(d && d != ''){
		// d = d.substring(0,10);
		d = parseInt(d);
		var date = new Date(d);
		return date.Format(f);
	}
	return '';
}
function parseCastingTime(d){
	if(d && d != '' ){
		d = d.replace(/-/ig,'/').replace(/\.\d{3}/,'');
		var date = new Date(d);
		return date;
	}
	return null;
}

Date.prototype.Format = function (fmt) { //author: meizz
	if(fmt == "null"){
		return "未知";
	}
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒

    };
    if (/(y+)/.test(fmt))
    	fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
   	 if (new RegExp("(" + k + ")").test(fmt))
    	fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//验证码倒计时方法
var tf = true;
function CodeTime(){
	var tim = 59;
	$("#getCode").html("<b style='color:#ff0003; font-size:14px;'>"+tim+"</b>"+"s后重新发送");
	var time = setInterval( function(){
		tf = false;
			tim = tim - 1;
			$("#getCode").html("<b style='color:red; font-size:14px;'>"+tim+"</b>"+"s后重新发送");
		
		if(tim == 0){
			clearInterval(time);
			$("#getCode").html("发送验证码");
			tf=true;
			};
	},1000);
	
}

//获取url的参数
function getParam(name){
    var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
    if(result == null || result.length < 1){
        return "";
    }
    return result[1];
}

var castingConfig = {
	"remotePre" : "https://www.doyiter.com/casting"   //http://47.93.91.48:8080/casting
}
if(self.location.href.indexOf("castingyou.cn") > -1){
	castingConfig.remotePre = "https://castingyou.cn/casting";
}